package com.example.demo.controller.surperController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.CommodityMapper;
import com.example.demo.Mapper.SurperMapper.CommoditySurperMapper;
import com.example.demo.base.ArrayCountUtils;
import com.example.demo.base.Result;
import com.example.demo.entity.Commodity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.example.demo.base.ArrayCountUtils.merge;

@RestController
@RequestMapping("/commoditySurper")
public class CommoditySurperController {
    @Resource
    CommoditySurperMapper commoditySurperMapper;

    /**
     * 获取数据库全部数据
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(commoditySurperMapper.selectList(null));
    }


    /**
     * 获取商品库存重量表并去重
      * @return
     */
    @GetMapping("/chatData")
    public Result<?> chatData() {
        List<Commodity> CommodityInfoList = commoditySurperMapper.selectList(null);
        List<ArrayCountUtils.BillsNums> billsNumsList = new ArrayList<>();
        for (Commodity com : CommodityInfoList){
            ArrayCountUtils.BillsNums billsNums = new ArrayCountUtils.BillsNums();
            billsNums.setId(com.getComName());
            billsNums.setSums(com.getWeight());
            billsNumsList.add(billsNums);
            billsNums = null;// 置空强制回收内存，以防数据量大内存泄露
        }
        List<ArrayCountUtils.BillsNums> res = merge(billsNumsList);
        return Result.success(res);
    }

    /**
     * 分页获取数据库数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageF(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Commodity> wrapper = Wrappers.<Commodity>lambdaQuery();
        Page<Commodity> commodityPage = commoditySurperMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(commodityPage);
    }

    /**
     * 添加记录
     *
     * @param comId
     * @param comName
     * @param kind
     * @param weight
     * @param firmName
     * @param expiration
     * @param note
     * @return
     */

    @GetMapping(value = "/add")
    public Result<?> saveC(@RequestParam String comId,
                           @RequestParam String comName,
                           @RequestParam String kind,
                           @RequestParam String weight,
                           @RequestParam String firmName,
                           @RequestParam String expiration,
                           @RequestParam String note,
                           @RequestParam String tenantId) {
        if (comId.isEmpty() || comName.isEmpty() || kind.isEmpty() || weight.isEmpty() || firmName.isEmpty() || expiration.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        Commodity commodity = new Commodity(Integer.valueOf(comId), comName, kind, Double.valueOf(weight), firmName, expiration, note,Integer.valueOf(tenantId));
        commoditySurperMapper.insert(commodity);
        return Result.success();
    }

    /**
     * 更新
     *
     * @param comId
     * @param comName
     * @param kind
     * @param weight
     * @param firmName
     * @param expiration
     * @param note
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> updateById(@RequestParam String comId,
                                @RequestParam String comName,
                                @RequestParam String kind,
                                @RequestParam String weight,
                                @RequestParam String firmName,
                                @RequestParam String expiration,
                                @RequestParam String note,
                                @RequestParam String tenantId) {
        if (comId.isEmpty() || comName.isEmpty() || kind.isEmpty() || weight.isEmpty() || firmName.isEmpty() || expiration.isEmpty() || note.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        Commodity commodity = new Commodity(Integer.valueOf(comId), comName, kind, Double.valueOf(weight), firmName, expiration, note,Integer.valueOf(tenantId));
        commoditySurperMapper.updateById(commodity);
        return Result.success();
    }

    /**
     * 商品溯源码查找
     *
     * @param id
     * @return
     */

    @GetMapping("/findById/{id}")
    public Result<?> findById(@PathVariable String id) {
        Commodity commodity = commoditySurperMapper.selectById(id);
        List<Commodity> commodityList = new ArrayList<>();
        if (commodity != null) {
            commodityList.add(commodity);
            return Result.success(commodityList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 分页模糊查找企业名称
     * @param pageNum
     * @param pageSize
     * @param comName
     * @return
     */

    @GetMapping(value = "/findByComName")
    public Result<?> findByComName(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam String comName) {
        LambdaQueryWrapper<Commodity> wrapper = Wrappers.<Commodity>lambdaQuery();
        wrapper.like(Commodity::getComName, comName);
        Page<Commodity> commodityPage = commoditySurperMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if(commodityPage.getTotal()==0) {
            return Result.error(0, "没有查询到相关数据");
        }
        return Result.success(commodityPage);
    }
//    /**
//     * 根据商品名称模糊查找
//     * @param comName
//     * @return
//     */
//    @GetMapping("/findByComName/{comName}")
//    public Result<?> findByComName(@PathVariable String comName) {
//        LambdaQueryWrapper<Commodity> wrapper = Wrappers.<Commodity>lambdaQuery();
//        wrapper.like(Commodity::getComName,comName);
//        List<Commodity> commodityList = commodityMapper.selectList(wrapper);
//        if(commodityList.size() > 0)
//            return Result.success(commodityList);
//        return Result.error(0, "没有查询到相关数据");
//    }

    @GetMapping(value = "/findByKind")
    public Result<?> findByKind(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam String kind) {
        LambdaQueryWrapper<Commodity> wrapper = Wrappers.<Commodity>lambdaQuery();
        wrapper.like(Commodity::getKind, kind);
        Page<Commodity> commodityPage = commoditySurperMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if(commodityPage.getTotal()==0) {
            return Result.error(0, "没有查询到相关数据");
        }
        return Result.success(commodityPage);
    }
//    /**
//     * 根据商品种类查找
//     *
//     * @param kind
//     * @return
//     */
//    @GetMapping("/findByKind/{kind}")
//    public Result<?> findByKind(@PathVariable String kind) {
//        LambdaQueryWrapper<Commodity> wrapper = Wrappers.<Commodity>lambdaQuery();
//        wrapper.like(Commodity::getKind, kind);
//        List<Commodity> commodityList = commodityMapper.selectList(wrapper);
//        if (commodityList.size() > 0)
//            return Result.success(commodityList);
//        return Result.error(0, "没有查询到相关数据");
//    }

    /**
     * 根据工厂名称查询
     *
     * @param firmName
     * @return
     */
    @GetMapping("/findByFirmName/{firmName}")
    public Result<?> findByFirmName(@PathVariable String firmName) {
        LambdaQueryWrapper<Commodity> wrapper = Wrappers.<Commodity>lambdaQuery();
        wrapper.like(Commodity::getFirmName, firmName);
        List<Commodity> commodityList = commoditySurperMapper.selectList(wrapper);
        if (commodityList.size() > 0)
            return Result.success(commodityList);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delById/{id}")
    public Result<?> delete(@PathVariable String id) {
        int res = commoditySurperMapper.deleteById(id);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

}
